package com.example.http_url_connection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.loopj.android.image.SmartImageView;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText;
    private ImageView imageView;
    private Button button;
    protected static final int CHANGE_UI = 1;
    protected static final int ERROR = 2;
    private ImageView imageView2;
    private Button button2;
    private Button button3;
    private SmartImageView smart_image;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == CHANGE_UI) {
                Bitmap bitmap = (Bitmap) msg.obj;

                imageView.setImageBitmap(bitmap);

            } else if (msg.what == ERROR) {
                Toast.makeText(MainActivity.this, "显示图片错误", Toast.LENGTH_LONG).show();
            }
        }

        ;
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        editText = (EditText) findViewById(R.id.editText);
        imageView = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);


        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(this);
        smart_image = (SmartImageView) findViewById(R.id.smart_image);
        smart_image.setOnClickListener(this);
    }

    private void submit() {
        // validate
        String editTextString = editText.getText().toString().trim();
        if (TextUtils.isEmpty(editTextString)) {
            Toast.makeText(this, "editTextString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }

    @Override
    public void onClick(View v) {
        final String path = editText.getText().toString().trim();
        if (TextUtils.isEmpty(path)) {
            Toast.makeText(this, "editTextString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //final String path = "http://s9.rr.itc.cn/r/wapChange/20167_1_14/a0n8904444338519855.png";
        switch (v.getId()) {
            case R.id.button:
                // String path = editText.getText().toString().trim();

                if (TextUtils.isEmpty(path)) {
                    Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show();
                } else {
                   Thread thread =new Thread() {
                        public void run() {

                            try {
                                URL url = new URL(path);  //创建URL对象
                                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                // 设置请求的方式
                                conn.setRequestMethod("GET");
                                //设置超时时间
                                conn.setConnectTimeout(5000);
                                Log.i("log", "123");
                                int code = conn.getResponseCode();
                                System.out.println(code);
                                Log.i("log", code + "");
                                if (code == 200) {
                                    InputStream is = conn.getInputStream();


                                    /*ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                                    byte[] data = new byte[1024];
                                                     int len = 0;
                                    InputStream inStream = conn.getInputStream();
                                                     while ((len = inStream.read(data)) != -1) {
                                                             outStream.write(data, 0, len);
                                                         }
                                                     inStream.close();
                                    System.out.println("数据："+new String(outStream.toByteArray()));*/




                                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                                    //iv.setImageBitmap(bitmap);
                                    Message msg = new Message();
                                    msg.what = CHANGE_UI;
                                    msg.obj = bitmap;
                                    handler.sendMessage(msg);
                                } else {

                                    Message msg = new Message();
                                    msg.what = ERROR;
                                    handler.sendMessage(msg);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                Message msg = new Message();
                                msg.what = ERROR;
                                handler.sendMessage(msg);
                            }
                        }


                    };

                   thread.start();
                   thread.run();
                }


                break;
            case R.id.button2:
                // 方法二：
                Glide.with(this).load(path).asBitmap().into(imageView);


                break;
            case R.id.button3:
                // 方法三：
                smart_image.setImageUrl(path);
                break;
        }
    }
}
